using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Collections;

using ComLib;
using ComLib.Caching;
using ComLib.Account;
using ComLib.Notifications;
using ComLib.Web.Lib.Models;

namespace ComLib.CMS.Areas.Admin
{
    [Authorize(Roles = "Admin")]
    public class UserController : Controller
    {
        /// <summary>
        /// Get list of all the entries in the Cache.
        /// </summary>
        /// <returns></returns>
        public ActionResult Index(int ? page)
        {
            int pageIndex = ( page == null) ? 1 : (int)page;
            PagedList<User> items = ComLib.Account.User.Get(pageIndex, 10);
            return View("List", new EntityListViewModel<User>(items, "/admin/user/index/", true));
        }


        /// <summary>
        /// Remove the cache entry with the supplied name.
        /// </summary>
        /// <param name="cacheKey"></param>
        /// <returns></returns>
        public ActionResult Approve(string userName)
        {
            ComLib.Account.User.Approve(userName);
            return Index(null);
        }


        /// <summary>
        /// Lock the user out of the system.
        /// </summary>
        /// <returns></returns>
        public ActionResult LockOut(string userName)
        {
            ComLib.Account.User.LockOut(userName, string.Empty);
            return Index(null);
        }


        /// <summary>
        /// Unlock a user from the system if he is locked out.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ActionResult UndoLockOut(string userName)
        {
            ComLib.Account.User.UndoLockOut(userName, string.Empty);
            return Index(null);
        }


        /// <summary>
        /// Send the user a password reminder.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ActionResult SendPasswordReminder(string userName)
        {
            User user = ComLib.Account.User.Get(userName);
            Notifier.RemindUserPassword(user.Email, "Password reminder", user.UserName, user.UserName, user.Password);
            return Index(null);
        }


        /// <summary>
        /// Increment the users warning count which goes up when he does something not allowed.
        /// e.g. Spamming users.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public ActionResult Warn(string userName)
        {
            // Not implemented.
            return Index(null);
        }
    }
}
